package com.thread.Priority;
/*
线程的优先级：先设置，再启动
如果出现：优先级低的线程先执行：优先级倒置，造成性能低下
 */
public class TestPriority {
    public static void main(String[] args) {
        //2、打印主线程的默认优先级，主线程优先级不能改变
        System.out.println(Thread.currentThread().getName()+"主线程main的优先级："+Thread.currentThread().getPriority());
        //通过接口创建线程对象，并将线程初始化
        MyPriority myPriority = new MyPriority();
        Thread t1 = new Thread(myPriority);
        Thread t2 = new Thread(myPriority);
        Thread t3 = new Thread(myPriority);
        Thread t4 = new Thread(myPriority);
        Thread t5 = new Thread(myPriority);
        Thread t6 = new Thread(myPriority);

        //3、先调整线程优先级，再启动线程，观察。注意：先后次序
        t1.setPriority(3);
        t1.start();

        t2.setPriority(1);
        t2.start();

        t3.setPriority(Thread.MIN_PRIORITY);
        t3.start();

        t4.setPriority(6);
        t4.start();

        t5.setPriority(5);
        t5.start();

        t6.setPriority(Thread.MAX_PRIORITY);//MAX_PRIORITY=10,优先级最高，大于10，小于0会抛出异常
        t6.start();


    }
}
class MyPriority implements Runnable{

    @Override
    public void run() {
        //1、重写Runnable接口的run方法：获取当前线程的名字，打印当前线程的优先级
        System.out.println(Thread.currentThread().getName()+"——>"+Thread.currentThread().getPriority());
    }
}
